﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NearForums.Services;
using System.Web.Mvc;
using NearForums.Web.Controllers.Filters;
using NearForums.Web.Controllers.Helpers;
using NearForums.Validation;
using NearForums.Web.Extensions;

namespace NearForums.Web.Controllers
{
    public class AskController:BaseController
    {
        private readonly IAskQuestionService _questionSvr;
        private readonly IAskAnswersService _answerSvr;
        private readonly IAskDiscuessService _discussSvr;
        private readonly IUsersService _userService;
        private readonly ITagsService _tagsService;

        public AskController(IAskQuestionService questionService, IAskAnswersService answerService, 
                            IAskDiscuessService discussService, ITagsService tagsService, IUsersService userservice)
            : base(userservice)
        {
            _questionSvr = questionService;
            _answerSvr = answerService;
            _discussSvr = discussService;
            _tagsService = tagsService;
            _userService = userservice;
        }

        #region question List
        public ActionResult List()
        {
            var list = _questionSvr.GetAllQuestionsByPage(10, 1);

            return View("Main", list);
        }

        public ActionResult ListByPage(int pageSize, int pageNum)
        {
            var list = _questionSvr.GetAllQuestionsByPage(pageSize, pageNum);

            return View("Main", list);
        }
        #endregion

        #region Add Question
        [HttpGet]
        [RequireAuthorization]
        [PreventFlood]
        public ActionResult AddQuestion()
        {   
            //关联论坛 to do
            var question = new Questions();

            return View("EditQuestion", question);
        }

        [HttpPost]
        [RequireAuthorization]
        [ValidateInput(false)]
        [PreventFlood(typeof(RedirectToRouteResult))]
        [ValidateAntiForgeryToken]
        public ActionResult AddQuestion(string forum, [Bind(Prefix = "", Exclude = "")] Questions question, bool notify, string email)
        {
            try
            {
                SubscriptionHelper.SetNotificationEmail(notify, email, Session, Config, _userService);

                question.User = new User(User.Id, User.UserName);
                question.Name = DateTime.Now.ToString("yyMMddmmssfff");
                

                if (ModelState.IsValid)
                {
                    _questionSvr.Add(question, 1, question.User.Id);
                }
            }
            catch (ValidationException ex)
            {
                this.AddErrors(this.ModelState, ex);
            }
            var roles = _userService.GetRoles().Where(x => x.Key <= Role);
            ViewBag.UserRoles = new SelectList(roles, "Key", "Value");
            return View("EditQuestion", question);
        }
        #endregion

        #region Edit Question
        #endregion

        #region Question Detail
        [AddVisitQuestion]
        [ValidateReadAccess]
        public ActionResult DetailQuestion(int id, int pageNum)
        {
            Questions question = _questionSvr.GetQuestionById(id);

            if(question == null)
                return ResultHelper.NotFoundResult(this);

            question.Answers = _answerSvr.GetAnswersByQuestion(question.Id);

            return View("Detail",question);
        }
        #endregion

        #region Add Answer
        [HttpGet]
        [RequireAuthorization]
        [PreventFlood]
        public ActionResult AddAnswer()
        {
            //关联论坛 to do
            var answer = new Answers();

            return View("EditAnswer", answer);
        }

        [HttpPost]
        [RequireAuthorization]
        [ValidateInput(false)]
        [PreventFlood(typeof(RedirectToRouteResult))]
        [ValidateAntiForgeryToken]
        public ActionResult AddAnswer(int question, [Bind(Prefix = "", Exclude = "")] Answers answer, bool notify, string email)
        {
            try
            {
                SubscriptionHelper.SetNotificationEmail(notify, email, Session, Config, _userService);

                answer.User = new User(User.Id, User.UserName);
                answer.Question = _questionSvr.GetQuestionById(question);

                if (ModelState.IsValid)
                {
                    _answerSvr.Add(answer, question, answer.User.Id);
                    //SubscriptionHelper.SendNotifications(this, message, this.Config, _topicSubscriptionService);
                    //Redirect to the message posted
                    return new RedirectToRouteExtraResult(new { action = "DetailQuestion", controller = "Ask", id = question, pageNum = 1 }, "#answer" + answer.ID);
                }
            }
            catch (ValidationException ex)
            {
                this.AddErrors(this.ModelState, ex);
            }

            var roles = _userService.GetRoles().Where(x => x.Key <= Role);
            ViewBag.UserRoles = new SelectList(roles, "Key", "Value");
            return View("DetailQuestion", question);
        }
        #endregion
    }
}
